﻿using Direct.IDAL.Manager.UserCenter;
using DirectSell.Common.Filter;
using DirectSell.Common.Session;
using DirectSell.Model.Manager;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DirectSell.WebLayer.Areas.Manager.Controllers
{
    public class BackNetworkMapController : Controller
    {
        #region 依赖注入
        [Dependency]
        protected IBackNetworkmapRepository _repository { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 后台位置网络图
        /// </summary>
        /// <returns></returns>
        [ManagerFilter(IsCheck = true)]
        public async Task<ActionResult> Index(string code)
        {
            #region 加载数据

            string usercode = code == null ? "0001" : code;

            //第一层
            var firstFloor = await _repository.QueryFirstFloor(usercode);

            //第二层
            var left1 = await _repository.Querybehind(firstFloor.UserCode, 1);
            var right1 = await _repository.Querybehind(firstFloor.UserCode, 2);
            ViewBag.Left1 = left1;
            ViewBag.Right1 = right1;
            //第三层左
            if (left1 != null)
            {
                var left2_1 = await _repository.Querybehind(left1.UserCode, 1);
                var left2_2 = await _repository.Querybehind(left1.UserCode, 2);
                ViewBag.left2_1 = left2_1;
                ViewBag.left2_2 = left2_2;
            }
            //第三层右
            if (right1 != null)
            {
                var right2_1 = await _repository.Querybehind(right1.UserCode, 1);
                var right2_2 = await _repository.Querybehind(right1.UserCode, 2);
                ViewBag.right2_1 = right2_1;
                ViewBag.right2_2 = right2_2;
            }

            #endregion

            return View(firstFloor);
        }

        #endregion

        #region 分部视图
        /// <summary>
        /// 分布视图
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult> List(string code)
        {
            #region 加载数据
            string usercode = code == null ? "0001" : code;
            //第一层
            var UserInfo = await _repository.QueryFirstFloor(usercode);
            //第二层
            var left1 = await _repository.Querybehind(UserInfo.UserCode, 1);
            var right1 = await _repository.Querybehind(UserInfo.UserCode, 2);
            ViewBag.Left1 = left1;
            ViewBag.Right1 = right1;
            //第三层左
            if (left1 != null)
            {
                var left2_1 = await _repository.Querybehind(left1.UserCode, 1);
                var left2_2 = await _repository.Querybehind(left1.UserCode, 2);
                ViewBag.left2_1 = left2_1;
                ViewBag.left2_2 = left2_2;
            }
            //第三层右
            if (right1 != null)
            {
                var right2_1 = await _repository.Querybehind(right1.UserCode, 1);
                var right2_2 = await _repository.Querybehind(right1.UserCode, 2);
                ViewBag.right2_1 = right2_1;
                ViewBag.right2_2 = right2_2;
            }

            #endregion
            return PartialView(UserInfo);
        }
        #endregion

        #region 判断是否存在
        /// <summary>
        /// 判断会员是否存在
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<ManagerRespCode> IsExistence(string code)
        {
            var UserInfo = await _repository.QueryFirstFloor(code);
            if (UserInfo != null)
            {
                return ManagerRespCode.成功;
            }
            return ManagerRespCode.失败;
        }
        #endregion

        #region 返回上一层
        /// <summary>
        /// 返回上一层
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<string> GoBack(string code)
        {
            if (code == "0001")
            {
                return null;
            }
            var UserInfo = await _repository.QueryFirstFloor(code);
            if (!string.IsNullOrEmpty(UserInfo.ContactCode))
            {
                return UserInfo.ContactCode;
            }
            return null;
        }
        #endregion

    }
}